<template>
  <div id="usr">
    <div class="usr-search">
      <el-col :span="20" style="margin-right: 20px">
        <el-input
            placeholder="请输入员工姓名 \ 员工工号"
            v-model="input"
            clearable
            @keyup.enter.native="searchUsr">
        </el-input>
      </el-col>
      <el-button icon="el-icon-search" circle @click="searchUsr"></el-button>
    </div>

    <div class="usr-info">
      <el-table
          :data="tableData"
          :header-cell-style="{'text-align':'center'}"
          :cell-style="{'text-align':'center'}"
          style="width: 100%; padding: 35px 10px;">
        <el-table-column
            label="员工编号"
            prop="userId"
            width="100">
        </el-table-column>
        <el-table-column
            label="登录帐号"
            prop="account"
            width="130">
        </el-table-column>
        <el-table-column
            label="姓名"
            prop="userName"
            width="130">
        </el-table-column>
        <el-table-column
            label="性别"
            prop="sex"
            width="80">
        </el-table-column>
        <el-table-column
            label="联系方式"
            prop="tel"
            width="200">
        </el-table-column>
        <el-table-column
            label="电子邮箱"
            prop="email"
            width="220">
        </el-table-column>

        <el-table-column
            align="right">
          <template slot-scope="scope">
            <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible" width="600px" :modal-append-to-body="false" style="overflow: hidden;">
              <el-form label-width="400">
                <el-form-item label="员工编号" :label-width="formLabelWidth">
                  <el-input v-model="usr.userId" autocomplete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="登录账号" :label-width="formLabelWidth">
                  <el-input v-model="usr.account" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="usr.userName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" :label-width="formLabelWidth">
                  <el-select v-model="usr.sex">
                    <el-option label="男" value="男"></el-option>
                    <el-option label="女" value="女"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="联系方式" :label-width="formLabelWidth">
                  <el-input v-model="usr.tel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电子邮箱" :label-width="formLabelWidth">
                  <el-input v-model="usr.email" autocomplete="off"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateUsr();dialogFormVisible = false;">确 定</el-button>
              </div>
            </el-dialog>
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row);dialogFormVisible = true">修改
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="usr-info-pages">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=page.current
            :page-sizes="[5, 10, 30]"
            :page-size=page.size
            layout="total, sizes, prev, pager, next, jumper"
            :total=page.total>
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
	
  name: "Usr",
  data() {
    return {
      input: '',
      tableData: [],
      checked: false,
      page: {
        current: 1,
        size: 5,
        pages: 0,
        total: 0,
        records: []
      },
      usr: {
        userId: 0,
        account: '',
        userName: '',
        sex: '',
        tel: '',
        email: '',
      },
      dialogFormVisible: false,
      formLabelWidth: '80px'
    }
  },
  watch: {
    deep: true,
    dialogFormVisible(newVal, oldVal) {
      if (!newVal) {
        setTimeout(function () {
          location.reload()
        }, 1000)
      }
    }
  },
  methods: {
    searchUsr() {
      this.$axios
          .get("/userInfo/searchUsr", {
            params: {
              info: this.input,
              current: 1,
              size: this.page.size
            },
            headers: {"token": localStorage.getItem("token")}
          })
          .then((resp) => {
            this.tableData = resp.data.data.records;
            this.page.total = resp.data.data.total;
            this.page.current = 1;
          })
    },
    handleEdit(index, row) {
      this.usr = row;
    },
    updateUsr() {
      this.$axios
          .put("/userInfo/updateUsr", this.usr, {headers: {"token": localStorage.getItem("token")}})
          .then((resp) => {
            if (resp.data.flag){
              this.$message({
                type: 'success',
                message: '用户信息修改成功!'
              });
            }else {
              this.$message({
                type: 'error',
                message: '用户信息修改失败!'
              });
            }
          })
    },
    handleDelete(index, row) {
      this.$confirm('确定注销该用户账号?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios
            .delete("/userInfo/dropUsr", {
              params: {
                userId: row.userId
              },
              headers: {"token": localStorage.getItem("token")}
            })
            .then((resp) => {
              if (resp.data.flag) {
                this.$message({
                  type: 'success',
                  message: '成功注销该账号!'
                });
              }
            })
        setTimeout(function () {
          location.reload()
        }, 1000)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消注销操作'
        });
      });
    },
    handleSizeChange(val) {
      this.page.size = val;
      if (this.input !== '') {
        this.$axios
            .get("/userInfo/searchUsr", {
              params: {
                info: this.input
              },
              headers: {"token": localStorage.getItem("token")}
            })
            .then((resp) => {
              this.tableData = resp.data.data;
              this.page.total = resp.data.data.length;
            })
      } else {
        this.$axios
            .get("/userInfo/allUsr", {
              params: {
                current: this.page.current,
                size: this.page.size
              },
              headers: {"token": localStorage.getItem("token")}
            })
            .then((resp) => {
              this.tableData = resp.data.data.records;
            })
      }
    }
    ,
    handleCurrentChange(val) {
      this.page.current = val;
      if (this.input !== '') {
        this.$axios
            .get("/userInfo/searchUsr", {
              params: {
                info: this.input
              },
              headers: {"token": localStorage.getItem("token")}
            })
            .then((resp) => {
              this.tableData = resp.data.data;
              this.page.total = resp.data.data.length;
            })
      } else {
        this.$axios
            .get("/userInfo/allUsr", {
              params: {
                current: this.page.current,
                size: this.page.size
              },
              headers: {"token": localStorage.getItem("token")}
            })
            .then((resp) => {
              this.tableData = resp.data.data.records;
            })
      }
    }
  },
  created() {
    this.$axios
        .get("/userInfo/allUsr", {
          params: {
            current: this.page.current,
            size: this.page.size
          },
          headers: {"token": localStorage.getItem("token")}
        })
        .then((resp) => {
          this.tableData = resp.data.data.records;
          this.page.total = resp.data.data.total;
        })
  }
}
</script>

<style scoped>
#usr {
  width: 100%;
  padding-top: 20px;
}

.usr-search {
  width: 400px;
  height: 40px;
  margin: 0 auto;
}

.usr-info {
  max-width: 1100px;
  margin: 20px auto;
  padding: 20px 20px;
}

.usr-info-pages {
  width: 550px;
  margin: 20px auto;
  background-color: #ffffff;
  padding: 5px 10px;
  border-radius: 10px;
}

/deep/ .el-table {
  background-color: rgba(255,255,255,0.6);
}

/deep/ .el-input__inner {
  text-align: center;
}

/deep/ .el-form {
  width: 400px;
  margin: 0 auto;
}
</style>